package controllers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import play.cache.Cache;
import services.AuthorityService;
import services.IndexService;
import services.OperatorService;
import services.RegionLevelRelationService;
import services.RegionService;
import services.RoleMenuService;
import services.RoleUserService;
import utils.BizConstants;
import utils.Node;
import utils.RegionTreeUtil;
import vos.MenuSearchVo;
import vos.OperatorSearchVo;
import vos.RegionLevelRelationVo;
import vos.ResultVo;
import vos.ReturnCode;
import vos.ReturnListVo;
import vos.SystemMenuView;
import models.Operator;
import models.Oplog;
import models.Region;
import models.RegionLevel;
import models.RegionLevelRelation;
import models.Role;
import models.SystemAuthority;
import models.SystemMenu;

public class RegionLevelRelationController extends BaseController {
	/**
	 * 跳转到城市等级分配页面
	 *
	 * @Title: roleUser 
	 * @return: void
	 */
	public static void regionLevelRelation(){
		render();
	}
	
	/**
	 * 保存城市等级关系
	 *
	 * @Title: saveRoleUser 
	 * @param levelid 城市等级id
	 * @param ids 菜单id数组
	 * @return: void
	 */
	public static void saveRegionLevelRelation(String levelid,String ids){
		if(StringUtils.isEmpty(levelid)){
			renderFailedJson(ReturnCode.ErrorCode,"请选择城市等级");
		}
		Set<String> regionSet=new HashSet<String>();
		if(StringUtils.isNotEmpty(ids)){
			String[] regions=ids.split(",");
			Collections.addAll(regionSet, regions);
		}
		else{
			regionSet=null;
		}
		List<RegionLevelRelation> listMap=new ArrayList<RegionLevelRelation>();
		//构建实体集合
		Map<String,List<RegionLevelRelation>> smap=new HashMap<String,List<RegionLevelRelation>>();
		if(regionSet!=null&&regionSet.size()>0){
			Iterator it = regionSet.iterator();
			while(it.hasNext()){
				String id=(String) it.next();
				RegionLevelRelationVo vo=RegionLevelRelationService.getByRegionId(id);
				if(!levelid.equals(vo.levelid)){
					System.out.println(vo.levelname);
					renderFailedJson(ReturnCode.ErrorCode,vo.text+"已设置为"+vo.levelname);
				}
				RegionLevelRelation relation=new RegionLevelRelation(levelid,id);
				listMap.add(relation);
			}
		}
		else{
			listMap=null;
		}
		smap.put("lists", listMap);
		boolean isok=RegionLevelRelationService.saveRegionLevelRelation(levelid,smap);
		if(isok){
			Operator oper=getLoginUser();
			Oplog oplog=new Oplog(oper.operatorid,"为城市等级"+levelid+"添加城市："+ids,"等级划分");
			oplog.save(oplog);
			renderFailedJson(ReturnCode.SuccessCode,"保存成功");
		}
		else{
			renderFailedJson(ReturnCode.ErrorCode,"保存失败");
		}
	}
	/**
	 * 获取城市等级列表
	 *
	 * @Title: getRegionLevel 
	 * @return: void
	 */
	public static void getRegionLevel(){
		List<RegionLevel> levels=RegionLevelRelationService.getRegionLevel();
		renderJSON(levels);
	}
	/**
	 * 获取选中等级的城市列表
	 *
	 * @Title: getCheched 
	 * @return: void
	 */
	public static void getChecked(String levelid){
		if(StringUtils.isEmpty(levelid)){
			renderJSON(null);
		}
		List<String> list=RegionLevelRelationService.getSelectRegion(levelid);
		renderJSON(list);
	}
	/**
	 * 获取城市列表
	 *
	 * @Title: getRegions 
	 * @return: void
	 */
	public static void getRegions(String levelid){
		List<String> checkregion=RegionLevelRelationService.getSelectRegion(levelid);
		//从缓存中获取城市列表
		List<Node> nodes=new ArrayList<Node>();
		String nodestr=(String)Cache.get(BizConstants.CACHE_REGION);
		if(StringUtils.isNotEmpty(nodestr)){
			nodes=JSONObject.parseArray(nodestr,Node.class);
		}
		//缓存中城市列表不存在时从数据库获取
		if(nodes==null||nodes.size()<=0){
			nodes=RegionTreeUtil.getRegionsTree();
		}
		//检查哪些节点被选中
		for(Node node :nodes){
			if(checkregion.contains(node.id)){
				node.checked=true;
			}
			if(node.children.size()>0){
				for(Node n: node.children){
					if(checkregion.contains(n.id)){
						n.checked=true;
					}
					if(n.children.size()>0){
						for(Node no: n.children){
							if(checkregion.contains(no.id)){
								no.checked=true;
							}
						}
					}
				}
			}
		}
		renderJSON(nodes);
	}
}
